package net.system.sys.action;

import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;

import org.apache.struts2.StrutsTestCase;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.opensymphony.xwork2.ActionProxy;


public class LoginActionTest extends StrutsTestCase {

	@Before
	public void setUp() throws Exception {
		// SessionFactory sessionFactory = lookupSessionFactory(request);
		// Session hibernateSession = getSession(sessionFactory);
		// TransactionSynchronizationManager.bindResource(sessionFactory, new
		// SessionHolder(hibernateSession));
	}

	@Test
	public void testGetActionMapping() {
		ActionMapping mapping = getActionMapping("/system/login/login.shtml");
		assertNotNull(mapping);
		assertEquals("/system/login", mapping.getNamespace());
		assertEquals("login", mapping.getName());
	}

	@Test
	public void testGetQusetionByQuestionId() {
		// 传入的参数
		request.setParameter("subject", "mathScore");
		// action的地址
		try {
			String reslt = executeAction("/stu!getAll");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	public void testLogin() {
		ActionProxy proxy = null;
		LoginAction test = null;
		String reg = "2355";
		request.setParameter("regCode", reg);
		request.getSession().setAttribute("reg", reg);
		proxy = getActionProxy("/system/login/login.shtml");
		test = (LoginAction) proxy.getAction();

		String result = "";
		try {
			result = proxy.execute();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		assertEquals("success", result);
		assertEquals("test...", test.getPassword());
	}

	@Test
	public void testDict() {
		fail("Not yet implemented");
	}

	@Test
	public void testSaveReg() {
		fail("Not yet implemented");
	}

//	// 获得sessionFactory
//	private SessionFactory lookupSessionFactory(HttpServletRequest request) {
//		try {
//			System.out.println(this.applicationContext);
//			return (SessionFactory) this.applicationContext.getBean("sessionFactory");
//		} catch (Exception e) {
//			e.printStackTrace();
//			return null;
//		}
//	}
//
//	// 获得session
//	private Session getSession(SessionFactory sessionFactory) {
//		Session session = SessionFactoryUtils.getSession(sessionFactory, true);
//		FlushMode flushMode = FlushMode.NEVER;
//		if (null != flushMode) {
//			session.setFlushMode(flushMode);
//		}
//		return session;
//	}

}
